 var actc_xml = null;
var hours = [1,2,3,4,5,6,7,8,9,10,11,12]
var minutes = new Array();
var schools = ["AUG","HAM","MAC","SCU-SP","SCU-MPLS","UST"];
var timeSelected = false;
$(document).ready(
	function(){
	gadgetHTML =   $("#xmlHolder").html();
	$("#xmlHolder").click(function(){reloadClickListeners()});
	$("th").click(function(){reloadClickListeners()});
	$("#xmlTD").click(function(){reloadClickListeners()});
	$("#resetChanges").click(function(){location.reload();});
	$("#showInstructions").click(function(){
	reloadClickListeners();
	if( $("#instructions").is(":hidden")){
	$("#instructions").slideDown()
	}else{
	$("#instructions").slideUp()
	}
	});
	for(var i =0; i < 61; i++){
	minutes.push(i);
	}
	tdClickHandler();
	cellToolClickHandler();
	
    removeMilitary(".time1");
	removeMilitary(".time2");
}
		
		
	
);
function getNewCellHTML(button){
var newCellTime = $(button).parent().siblings().first().attr("class")
var newCellPlace = $(button).parent().siblings().last().attr("class")
var newCellHTML = '<tr><td><button id="delete" class="button">&#935;</button><button id="oneAbove" class="button" >&#8593;</button><button id="oneBellow" class="button" >&#8595;</button></td>'+
				  '<td class="'+newCellTime+'">1:00 am</td>' +
				  '<td class="'+ newCellPlace +'">Select a Location</td></tr>';
return newCellHTML;
}

function reloadClickListeners(){
			placeClickEvent($("#curSel.selectList").parent());
			timeClickEvent($("#curSel.hour").parent());
			if(timeSelected){
			timeText = getFormatedTime()
			$("#curSel.hour").parent().html(timeText);
			}else{
			var selectedValue = $("#curSel").val();
			$("#curSel.selectList").parent().html(selectedValue);
			}

}
function cellToolClickHandler(){
		$("#genXML").click(function(){
		generateXML();
		if($("#selectAll").length < 1){
		var selAllButton = '<button onclick="selectText("xmlHolder");">Select XML</button>';
		$("#genXML").parent().append('<button id="selectAll">Select all XML</button>');
		$("#selectAll").click(function(){ selectText("xmlHolder")});
		}
		});
		$("*").each(function(){
		var curAttr = $(this).attr("id") 
		if(curAttr == "delete"){
		deleteButton(this);
		}
		if(curAttr == "oneAbove"){
		insertAbove(this);
		}
		if(curAttr == "oneBellow"){
		insertBellow(this);
		}
		});
}
function deleteButton(button){
			$(button).unbind();
			$(button).click(function(){
			var time = $(button).parent().siblings().first().text();
			var stop = $(button).parent().siblings().last().text();
			var bus = $(button).parent().siblings().attr("class").split("e")[1];
			var ok = confirm("Are you shure you want to delete this row?\nBus " + bus +"\nTime: " + time + "\nStop: " + stop);
			if(ok){
			$(this).parent().parent().remove();
			}
			});
}
function insertAbove(button){
			$(button).unbind();
			$(button).click(function(){
			var newCellHTML = getNewCellHTML(this);
			$(newCellHTML).insertBefore($(this).parent().parent());
			cellToolClickHandler();
			tdClickHandler()
			});

}
function insertBellow(button){
		  $(button).unbind();
			$(button).click(function(){
			var newCellHTML = getNewCellHTML(this);
			$(newCellHTML).insertAfter($(this).parent().parent());
			cellToolClickHandler();
			tdClickHandler();});
			}

  
function tdClickHandler(){
	$(".time1").each(function(){$(this).unbind();timeClickEvent(this);});
	$(".place1").each(function(){$(this).unbind();placeClickEvent(this);});
	$(".time2").each(function(){$(this).unbind();timeClickEvent(this);});
	$(".place2").each(function(){$(this).unbind();placeClickEvent(this);});
}
  
function placeClickEvent(buttomThatIsClicked){
	$(buttomThatIsClicked).click(
		function(){	
		reloadClickListeners();
  			var value = $(this).html();
  			$(this).unbind('click');
  			var selectListHTML = "<select id='curSel' class='selectList'>"
  			for(var i = 0; i < schools.length; i++){
  				if(value == schools[i])
  					selectListHTML += "<option selected='selected' value='"+ schools[i] + "'>" + schools[i] + "</option>";
  				else
  					selectListHTML += "<option value='"+ schools[i] + "'>" + schools[i] + "</option>";
  				}
  			selectListHTML += "</select>";
  			$(this).html(selectListHTML);
   		});
  }
function timeClickEvent(buttomThatIsClicked){
	$(buttomThatIsClicked).click(
		function(){	
			reloadClickListeners();
			
			timeSelected = true;
			
			  var currentItem = null;
  			var minutesList = null;
  			var amPm = null
 
  			var value = $(this).html().split(" ");
  			var curHour = value[0].split(":")[0];
  			amPm = "<select class='timeOfDay'>"
  			if(value[1] == "pm"){
  				amPm += "<option value='am'>am</option>";
  				amPm += "<option selected='selected' value='pm'>pm</option>";
  			}else{
  				amPm += "<option selected='selected'  value='am'>am</option>";
  				amPm += "<option value='pm'>pm</option>";
  			}
  			amPm += "</select>";
  			var curMin = value[0].split(":")[1];
  			$(this).unbind('click');
  			
  			
  			var hoursList = "<select id='curSel' class='hour'>";
  			for(var i =0; i < hours.length; i++){
  			if(hours[i] == curHour){
  			hoursList += "<option selected='selected' value='"+ hours[i] + "'>" + hours[i] + "</option>";
  			}else{
  			hoursList += "<option value='"+ hours[i] + "'>" + hours[i] + "</option>";
  			}
  			}
  			hoursList+= "</select>";
  			
  			minutesList = "<select  class='minute'>";
  			
  			for(var i =0; i < minutes.length; i++){
  			if(i < 10){
  			minuteString = "0" + i;
  			}else{
  			minuteString = i;
  			}
  			if(minutes[i] == curMin){
  			minutesList += "<option selected='selected' value='"+ minuteString + "'>" + minuteString + "</option>";
  			}else{
  			minutesList += "<option value='"+ minuteString + "'>" + minuteString + "</option>";
  			}
  			}
  			minutesList+= "</select>";
  			
  			
  			//$(this).html('<input type="text" id="curSel" value="'+ value +'"/>');
  			$(this).html(hoursList + ":");
  			$(this).append(minutesList);
  			$(this).append(amPm);
  			currentItem = $(this)
   		});
  }

  function getFormatedTime(){
	timeSelected = false;
  time = $(".hour").val() + ":" + $(".minute").val() + " " + $(".timeOfDay").val();

  return time;
  }
function generateXML(){
while(errorCSS.length > 0){
$(errorCSS.pop()).parent().css("background-color","")
}
			placeClickEvent($("#curSel.selectList").parent());
			timeClickEvent($("#curSel.hour").parent());
			if(timeSelected){
			timeText = getFormatedTime()
			$("#curSel.hour").parent().html(timeText);
			}else{
			var selectedValue = $("#curSel").val();
			$("#curSel.selectList").parent().html(selectedValue);
			}
  
  	var time1 = new Array();
  	var time2 = new Array();
  	var place1 = new Array();
  	var place2 = new Array();
  	var error = false;
  	var errorMessage = '';
  	$(".time1").each(function(){time1.push(getMilitaryTime($(this).html()));});
  	$(".place1").each(function(){
  	errorMessage = validLocation(this)
  	if(errorMessage != "ok"){
  	error = true;
  	}
  	place1.push($(this).html());});
  	$(".time2").each(function(){time2.push(getMilitaryTime($(this).html()));});
  	$(".place2").each(function(){
  	errorMessage = validLocation(this)
  	if(errorMessage != "ok"){
  	error = true;
  	}
  	
  	place2.push($(this).html());});
  	actc_xml = '&lt;?xml version="1.0" encoding="UTF-8"?>' +
	'<br>&lt;?xml-stylesheet type="text/xsl" href="actc_schedule.xsl"?>'+
 '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ACTC>' +
 '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Bus1>';
 	for(var i = 0; i < time1.length; i++){
		 actc_xml += "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;stop>&lt;time>"+time1[i]+"&lt;/time>&lt;place>"+place1[i]+"&lt;/place>&lt;/stop>";
	 }
	 actc_xml +=  '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt/Bus1>'+
  				  '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&ltBus2>'; 
	for(var i = 0; i < time2.length; i++){
 		actc_xml += "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stop>&lt;time>"+time2[i]+"&lt;/time>&lt;place>"+place2[i]+"&lt;/place>&lt;/stop>";
 	}
  
  actc_xml +=  '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/Bus2>'+
  			  '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/ACTC>'; 
  			  
  
  if(error){
   $("#xmlHolder").html(foundErrorMessage);
   }else{
  $("#xmlHolder").html(actc_xml);

  }
	
}
var errorCSS = new Array();
var foundErrorMessage = '';
function validLocation(listBox){
var errorMessage = "ok"
if($(listBox).html() == "Select a Location"){
errorMessage = "please select a valid location";
foundErrorMessage = errorMessage;

errorCSS.push(listBox);
$(listBox).parent().css("background-color","#f11")
}
return errorMessage
}
function selectText(element) {
    var doc = document;
    var text = doc.getElementById(element);    

    if (doc.body.createTextRange) { // ms
        var range = doc.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) { // moz, opera, webkit
        var selection = window.getSelection();            
        var range = doc.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
    }
function removeMilitary(className){
$(className).each(function(){  
var time = $(this).text().split(" ");
var hour = time[0].split(":")[0];
var minute = time[0].split(":")[1];
var amPM = time[1];
var newTime = $(this).text();
if(hour > 11){
amPM = "pm";
}
if(hour > 12){
hour = hour - 12;
}
$(this).text(hour + ":" + minute + " " + amPM);
});
}
function getMilitaryTime(normalTime){
var hour = normalTime.split(" ")[0].split(":")[0];
var minute = normalTime.split(" ")[0].split(":")[1];
var amPM = normalTime.split(" ")[1]
var time = '';
if(amPM == "pm"){
if(hour == 12){
time = hour + ":" + minute;
}else{
hour = parseInt(hour) + 12;
time = hour + ":" + minute;
}

}else{
time = hour + ":" +minute;
}
return time;
}

 

